package com.system.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ImageUtil {
    private static final String ENDPOINT = "oss-cn-hangzhou.aliyuncs.com";
    private static final String BUCKET_NAME = "wyyy-zlich21k";
    private static final String ACCESS_KEY_ID = "LTAI5tD9JBBgrU6ro7jf1fYd";
    private static final String ACCESS_KEY_SECRET = "TNI4rM3zcQKOTYUORju4mITyDr6qav";

    private static final OSS ossClient = new OSSClientBuilder().build("http://" + ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);


    public static String upload(InputStream is, String fileName) {
        String newFileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, newFileName, is);
        PutObjectResult result = ossClient.putObject(request);
        return "https://" + BUCKET_NAME + "." + ENDPOINT + "/" + newFileName;

    }

    public static List<String> getFileList(){
        List<String> list = new ArrayList<>();
        ListObjectsRequest request = new ListObjectsRequest(BUCKET_NAME);
        request.setMarker(null);
        request.setMaxKeys(1000);
        ObjectListing objectListing = ossClient.listObjects(request);
        for(OSSObjectSummary ossObjectSummary:objectListing.getObjectSummaries()){
            list.add(ossObjectSummary.getKey());
        }
        return list;

    }

    public static void delete(String fileName){
        ossClient.deleteObject(BUCKET_NAME,fileName);
    }

    public static void deleteList(List<String> flieNameList){
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(BUCKET_NAME);
        deleteObjectsRequest.setKeys(flieNameList);
        ossClient.deleteObjects(deleteObjectsRequest);
    }


    public static void main(String[] args) {

//        try {
//            MultipartFile multipartFile = null;
////            multipartFile.getInputStream();
//            File file = new File("D:\\321.png");
//            InputStream is = new FileInputStream(file);
//            String newFileName = upload(is, file.getName());
//            System.out.println(newFileName);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        System.out.println(getFileList());
        delete("0e9866f2-8ca9-4ac4-9f0f-7e4dbdd5a3d2.png");
        System.out.println(getFileList());
    }
}



